package org.example.les01;

import java.io.IOException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

/**
 * 基于UDP的一个字典服务
 */
public class Demo01_UDPDictServer extends Demo01_UDPEchoServer {
    //定义一个字典的map
    private Map<String,String> map;
    //初始化
    public Demo01_UDPDictServer(int port) throws SocketException {
        super(port);
        //初始化字典内容
        map=new HashMap<>();
        map.put("cat","猫");
        map.put("dog","狗");
        map.put("panda","熊猫");
        map.put("pig","猪");
    }

    @Override
    protected String process(String key) {
        String response=map.getOrDefault(key,"查无此词");
        return response;
    }

    public static void main(String[] args) throws IOException {
        Demo01_UDPDictServer server=new Demo01_UDPDictServer(8888);
        server.start();
    }
}
